import random

import pygame.sprite

from actor import WalkDir
from actor.walk_soldier import WalkSoldier

"""
日军战士类
"""


class JapaneseWalkSoldier(WalkSoldier):
    def __init__(self, x, y):
        """
        初始化操作
        :param x:
        :param y:
        """
        soldier_id = 4
        walk_image_path = "./resources/images/japanese/soldier4.png"
        super(JapaneseWalkSoldier, self).__init__(walk_image_path, x, y, "小日本" + str(soldier_id),
                                                  100)
        self.pos_y = y
        self.pos_x = x


"""
自定义派生类，实现上下行走
"""


class JapaneseWalkSoldierUpAndDown(WalkSoldier):
    def __init__(self, x, y):
        """
        初始操作
        :param x:
        :param y:
        """
        soldier_id = 1
        walk_image_path = "./resources/images/japanese/soldier1.png"
        super(JapaneseWalkSoldierUpAndDown, self).__init__(walk_image_path, x, y, "小日本" + str(soldier_id),
                                                           100)
        self.pos_y = y
        self.pos_x = x

    def run(self, down_flag, key_list, obstacle_group: pygame.sprite.Group):
        """
        实现移动，碰撞,以及方向的变更
        :param down_flag:
        :param key_list:
        :param obstacle_group: 障碍物
        :return:
        """
        off_x = 0
        off_y = 0
        if self.count == 0:
            self.count = 3
            if self.cur_dir == WalkDir.up:
                off_y -= 10
            elif self.cur_dir == WalkDir.down:
                off_y += 10
        else:
            self.count -= 1
        # 保存当前位置矩形
        temp_rect = self.rect
        self.rect = self.rect.move(off_x, off_y)
        # 碰撞
        collidate_list = pygame.sprite.spritecollide(self, obstacle_group, False)
        if len(collidate_list) > 0:
            self.rect = temp_rect
            if self.cur_dir == WalkDir.up:
                self.walk_action.set_dir(WalkDir.down)
                self.cur_dir = WalkDir.down
            else:
                self.walk_action.set_dir(WalkDir.up)
                self.cur_dir = WalkDir.up
        else:
            self.pos_y += off_y

    def is_collide(self, obstacle_group):
        """
        判断是否碰撞
        :param obstacle_group:
        :return:
        """
        return pygame.sprite.spritecollide(self, obstacle_group, False)


"""
实现左右移动的日军
"""


class JapaneseWalkSoldierLAndR(WalkSoldier):
    def __init__(self, x, y):
        """
        同上
        :param x:
        :param y:
        """
        soldier_id = 2
        walk_image_path = "./resources/images/japanese/soldier2.png"
        super(JapaneseWalkSoldierLAndR, self).__init__(walk_image_path, x, y, "小日本" + str(soldier_id),
                                                       100)
        self.pos_y = y
        self.pos_x = x

    def run(self, down_flag, key_list, obstacle_group: pygame.sprite.Group):
        """
        同上
        :param down_flag:
        :param key_list:
        :param obstacle_group:
        :return:
        """
        off_x = 0
        off_y = 0
        if self.count == 0:
            self.count = 3
            if self.cur_dir == WalkDir.left:
                off_x -= 10
            elif self.cur_dir == WalkDir.right:
                off_x += 10
            else:
                self.walk_action.set_dir(WalkDir.right)
                self.cur_dir = WalkDir.right
        else:
            self.count -= 1
        temp_rect = self.rect
        self.rect = self.rect.move(off_x, off_y)
        collidate_list = pygame.sprite.spritecollide(self, obstacle_group, False)
        if len(collidate_list) > 0:
            self.rect = temp_rect
            if self.cur_dir == WalkDir.left:
                self.walk_action.set_dir(WalkDir.right)
                self.cur_dir = WalkDir.right
            else:
                self.walk_action.set_dir(WalkDir.left)
                self.cur_dir = WalkDir.left
        else:
            self.pos_x += off_x
